package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.util.Md5;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    @Override
    public ResultModel add(UserModel userModel) {
//        UserDao userDao=new UserImpl();
//        ResultModel resultModel=new ResultModel();
//        resultModel.setCount(userDao.add(userModel));
//        resultModel.setData(null);
//        resultModel.setCode(0);
//        resultModel.setMsg("");
//
//        return resultModel;
//        Md5 md5=new Md5();
//        String mingwen = userModel.getPassword();
//        String anwen = Md5.encoderByMd5(mingwen);
//        userModel.setPassword(anwen);
        userModel.setPassword(Md5.encoderByMd5(userModel.getPassword()));
        return ResultModel.getResultModel(userDao.add(userModel));
    }

    @Override
    public ResultModel deltete(UserModel userModel) {
        return ResultModel.getResultModel(userDao.delete(userModel));
    }

    @Override
    public ResultModel update(UserModel userModel) {
        return ResultModel.getResultModel(userDao.update(userModel));
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
        return ResultModel.getResultModel(userDao.getCount(userModel), userDao.findAll(userModel));
    }

    @Override
    public ResultModel findById(UserModel userModel) {
        return ResultModel.getResultModel(userDao.findById(userModel));
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        return ResultModel.getResultModel(userDao.getCount(userModel));
    }

    @Override
    public ResultModel enable(UserModel userModel) {
        return ResultModel.getResultModel(userDao.enable(userModel));
    }

    /**
     * 登录逻辑处理
     * 1.验证验证码是否输入正确
     * 2.根据用户名和密码进行数据库查询，如果有当前用户，则可以进行登录
     * 3.否则返回用户名或者密码输入错我
     *
     * @param model
     * @return
     */
    @Override
    public ResultModel login(UserModel model) {
        if (model.getCode().equals(model.getSessionCode())) {
//            根据用户名和密码查询
            model.setPassword(Md5.encoderByMd5(model.getPassword()));
            UserModel userModel = userDao.findByUsernameAndPassword(model);
            if (userModel == null) {
                return ResultModel.getResultModel("error");
            } else {
                return ResultModel.getResultModel("success");
            }

        } else {
            return ResultModel.getResultModel("error-code");
        }

    }


}
